using AutoMapper;
using Core.SimpleTemp.Api.Mobile;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// 操作及相关作业活动
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/Mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class FX_OperationAndWorkController : MobileBaseConteroller
    {
        private readonly FX_OperationAndWorkService _service;
        /// <summary>
        /// FX_OperationAndWork
        ///</summary>
        public FX_OperationAndWorkController(FX_OperationAndWorkService service)
        {
            _service = service;
        }

        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        //[PermissionFilter(nameof(FX_OperationAndWorkController) + "_" + nameof(FX_OperationAndWorkController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<FX_OperationAndWorkDto> pagingQueryModel)
        {
            IPageModel<FX_OperationAndWorkDto>
                result;
            result = await _service.GetList(pagingQueryModel);

            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.JobSteps,//作业步骤
                a.JobTask,//作业任务
                a.RiskLevelText//作业任务
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var entity = await _service.GetAsync(id);
            if (entity == null) return this.JsonFaild("未找到对应数据:" + id);
            var dto = Mapper.Map<FX_OperationAndWorkDto>(entity);
            var retModel = new
            {
                dto.Id,
                dto.JobSteps,//作业步骤
                dto.JobTask,//作业任务
                dto.RiskLevelText,//风险级别Text
                dto.Risk,//风险
                dto.HazardousFactor,//危害因素
                dto.RequirementsComply,//现场作业行为和活动应遵守的要求
                dto.ControlRequirement//控制要求
            };
            return this.JsonSuccess(retModel);
        }
    }
}
